/**
 * DoubleScorePowerUp - 双倍分数道具
 * 在限定时间内让玩家获得双倍分数
 */

import { PowerUp, PowerUpType, PowerUpRarity, type PowerUpConfig, type PowerUpEffect, type PowerUpOptions } from '../PowerUp'
import type { Player } from '../Player'

/**
 * 双倍分数效果实现
 */
export class DoubleScoreEffect implements PowerUpEffect {
  private scoreMultiplier: number = 2.0
  // private originalScoreCalculation: (() => void) | null = null

  /**
   * 应用双倍分数效果
   */
  apply(_player: Player): void {
    // 在实际实现中，这里会修改玩家的分数计算逻辑
    // 由于Player类的分数计算在update方法中，我们需要修改分数计算方式
    
    console.log(`双倍分数效果已激活，分数倍数: ${this.scoreMultiplier}x`)
    
    // 这里可以设置一个标志，让游戏引擎知道当前处于双倍分数状态
    // 在实际的分数计算中会检查这个状态
  }

  /**
   * 移除双倍分数效果
   */
  remove(_player: Player): void {
    console.log('双倍分数效果已结束')
    
    // 恢复正常的分数计算
  }

  /**
   * 更新双倍分数效果
   */
  update(_player: Player, _deltaTime: number): void {
    // 双倍分数效果通常不需要每帧更新
    // 可以在这里添加特殊的视觉效果，比如分数文字的颜色变化
  }

  /**
   * 获取分数倍数
   */
  getScoreMultiplier(): number {
    return this.scoreMultiplier
  }

  /**
   * 计算修正后的分数
   */
  calculateScore(baseScore: number): number {
    return Math.floor(baseScore * this.scoreMultiplier)
  }
}

/**
 * 双倍分数道具类
 */
export class DoubleScorePowerUp extends PowerUp {
  constructor(options: Omit<PowerUpOptions, 'powerUpType'>) {
    super({
      ...options,
      powerUpType: PowerUpType.DOUBLE_SCORE
    })
  }

  /**
   * 创建双倍分数道具配置
   */
  protected createConfig(customConfig?: Partial<PowerUpConfig>): PowerUpConfig {
    const defaultConfig: PowerUpConfig = {
      type: PowerUpType.DOUBLE_SCORE,
      duration: 10000, // 10秒
      value: 2.0, // 分数倍数
      rarity: PowerUpRarity.RARE,
      stackable: false, // 双倍分数不能叠加
      maxStacks: 1,
      description: '在10秒内获得双倍分数',
      iconColor: '#FF9800' // 橙色
    }

    return { ...defaultConfig, ...customConfig }
  }

  /**
   * 创建双倍分数效果
   */
  protected createDefaultEffect(): PowerUpEffect {
    return new DoubleScoreEffect()
  }

  /**
   * 渲染双倍分数道具的特殊效果
   */
  protected renderDefault(ctx: CanvasRenderingContext2D): void {
    // 绘制数字"2"和星星装饰
    ctx.fillStyle = this.config.iconColor
    
    const centerX = 0
    const centerY = 0
    const size = Math.min(this.width, this.height) * 0.6
    
    // 绘制数字"2"
    ctx.font = `bold ${size}px Arial`
    ctx.textAlign = 'center'
    ctx.textBaseline = 'middle'
    ctx.fillText('2', centerX, centerY)

    // 绘制装饰星星
    ctx.fillStyle = '#FFB74D'
    this.drawStar(ctx, centerX - size/2, centerY - size/2, 6, 3)
    this.drawStar(ctx, centerX + size/2, centerY - size/2, 6, 3)
    this.drawStar(ctx, centerX - size/2, centerY + size/2, 6, 3)
    this.drawStar(ctx, centerX + size/2, centerY + size/2, 6, 3)

    // 绘制光芒效果
    ctx.strokeStyle = '#FFC107'
    ctx.lineWidth = 2
    
    for (let i = 0; i < 8; i++) {
      const angle = (i * Math.PI * 2) / 8
      const startX = centerX + Math.cos(angle) * (size/2 + 5)
      const startY = centerY + Math.sin(angle) * (size/2 + 5)
      const endX = centerX + Math.cos(angle) * (size/2 + 12)
      const endY = centerY + Math.sin(angle) * (size/2 + 12)
      
      ctx.beginPath()
      ctx.moveTo(startX, startY)
      ctx.lineTo(endX, endY)
      ctx.stroke()
    }
  }

  /**
   * 绘制星星形状
   */
  private drawStar(ctx: CanvasRenderingContext2D, x: number, y: number, outerRadius: number, innerRadius: number): void {
    const spikes = 5
    const step = Math.PI / spikes
    let rotation = Math.PI / 2 * 3
    
    ctx.beginPath()
    ctx.moveTo(x, y - outerRadius)
    
    for (let i = 0; i < spikes; i++) {
      const xOuter = x + Math.cos(rotation) * outerRadius
      const yOuter = y + Math.sin(rotation) * outerRadius
      ctx.lineTo(xOuter, yOuter)
      rotation += step
      
      const xInner = x + Math.cos(rotation) * innerRadius
      const yInner = y + Math.sin(rotation) * innerRadius
      ctx.lineTo(xInner, yInner)
      rotation += step
    }
    
    ctx.lineTo(x, y - outerRadius)
    ctx.closePath()
    ctx.fill()
  }
}